package com.pan.common.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * Created by panstark
 */
@Slf4j
public class PropertiesUtil {

    private static Properties props;


    static {
        String fileName = "icemall.properties";
        props = new Properties();
        try {
            props.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName),"UTF-8"));
        } catch (IOException e) {
            log.error("配置文件读取异常",e);
        }
    }

    public static String getProperty(String key){
        String value = props.getProperty(key.trim());
        if(StringUtils.isBlank(value)){
            return null;
        }
        return value.trim();
    }

    public static String getProperty(String key,String defaultValue){

        String value = props.getProperty(key.trim());
        if(StringUtils.isBlank(value)){
            value = defaultValue;
        }
        return value.trim();
    }

    public static Integer getPropertyInteger(String key,Integer defaultValue){

        String value = props.getProperty(key.trim());

        return StringUtils.isBlank(value)?Integer.parseInt(value.trim()):defaultValue;
    }

    public static Boolean getPropertyBoolean(String key,Boolean defaultValue){

        String value = props.getProperty(key.trim());

        return StringUtils.isBlank(value)?Boolean.valueOf(value):defaultValue;
    }



    /**
     * 小知识
     * 执行顺序：静态块>普通代码块>构造器代码块
     * static{
     *
     * }
     *
     * {
     *
     * }
     *
     * public 构造函数(){
     *
     * }
     */


}
